import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ScanAllPic {

	/**
	 * 获取所有指定后缀，指定分辨率的图片
	 *
	 * arg0 根目录,arg1 图片后缀, arg2 图片宽度, arg3 图片高度, arg4 要转换的图片宽度, arg5要转换的图片高度
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		if (args == null || args.length != 6) {
			return;
		}
		List<String> pics = new ArrayList<String>();
		getPics(args[0], args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]), pics);
		for (String pic : pics) {
			File f = new File(pic);
			String parentPath = f.getParent();

			String newName = args[4] + "x" + args[5] + ".png";

			System.out.println("正在转换" + pic + "到" + parentPath + "\\" + newName);

			try {
				ImageResize.resizeImage(pic, parentPath+"\\" + newName, Integer.parseInt(args[4]), Integer.parseInt(args[5]));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void getPics(String path, String format, int width, int height, List<String> pics) {
		if (pics == null) {
			pics = new ArrayList<String>();
		}
		File root = new File(path);
		File[] files = root.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				getPics(file.getAbsolutePath(), format, width, height, pics);
			} else {
				String p = file.getAbsolutePath();
				if (p.toLowerCase().endsWith(format)) {
					ImageSize imageSize = new ImageSize(p);
					if (imageSize.width == width && imageSize.height == height) {
						pics.add(file.getAbsolutePath());
					}
				}
			}
		}
	}

}
